package com.ruoyi.report.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.report.domain.ReportCarInfo;
import com.ruoyi.report.service.IReportCarInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 物流车信息Controller
 * 
 * @author ruoyi
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/report/car")
public class ReportCarInfoController extends BaseController
{
    @Autowired
    private IReportCarInfoService reportCarInfoService;
@Autowired
private TokenService tokenService;
    /**
     * 查询物流车信息列表
     */
    @PreAuthorize("@ss.hasPermi('report:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportCarInfo reportCarInfo)
    {
        startPage();
        List<ReportCarInfo> list = reportCarInfoService.selectReportCarInfoList(reportCarInfo);
        return getDataTable(list);
    }

    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage()
    {
        List<ReportCarInfo> list = reportCarInfoService.selectReportCarInfoList(new ReportCarInfo());
        return getDataTable(list);
    }

    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 导出物流车信息列表
     */
    @PreAuthorize("@ss.hasPermi('report:car:export')")
    @Log(title = "物流车信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,ReportCarInfo reportCarInfo)
    {
        SysDictData dict = new SysDictData();
        dict.setDictType("car_type");
        List<SysDictData> sysDictData = dictDataService.selectDictDataList(dict);
        List<ReportCarInfo> list = reportCarInfoService.selectReportCarInfoList(reportCarInfo);
        for (ReportCarInfo carInfo : list) {
            Long id = carInfo.getType();

            SysDictData correspondingDict = sysDictData.stream()
                    .filter(d -> d.getDictValue().equals(String.valueOf(id)))
                    .findFirst()
                    .orElse(null);
            carInfo.setTypeName(ObjectUtil.isNotEmpty(correspondingDict)?correspondingDict.getDictLabel():"");


        }


        ExcelUtil<ReportCarInfo> util = new ExcelUtil<ReportCarInfo>(ReportCarInfo.class);
         util.exportExcel(response,list, "物流车信息数据");
    }

    /**
     * 获取物流车信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:car:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(reportCarInfoService.selectReportCarInfoById(id));
    }

    /**
     * 新增物流车信息
     */
//    @PreAuthorize("@ss.hasPermi('report:car:add')")
    @Log(title = "物流车信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportCarInfo reportCarInfo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Long deptId = loginUser.getUser().getDeptId();
        Long deptId1 = reportCarInfo.getDeptId();
        if (loginUser.getUser().isAdmin()){
            if (ObjectUtil.isNull(deptId1)){
                throw new RuntimeException("请选择部门");
            }
        }else {
            reportCarInfo.setDeptId(deptId);
        }
        return toAjax(reportCarInfoService.insertReportCarInfo(reportCarInfo));
    }

    /**
     * 修改物流车信息
     */
//    @PreAuthorize("@ss.hasPermi('report:car:edit')")
    @Log(title = "物流车信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportCarInfo reportCarInfo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        Long deptId = loginUser.getUser().getDeptId();
        Long deptId1 = reportCarInfo.getDeptId();
        if (loginUser.getUser().isAdmin()){
            if (ObjectUtil.isNull(deptId1)){
                throw new RuntimeException("请选择部门");
            }
        }else {
            reportCarInfo.setDeptId(deptId);
        }
        return toAjax(reportCarInfoService.updateReportCarInfo(reportCarInfo));
    }

    /**
     * 删除物流车信息
     */
    @PreAuthorize("@ss.hasPermi('report:car:remove')")
    @Log(title = "物流车信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reportCarInfoService.deleteReportCarInfoByIds(ids));
    }
}
